package cn.ym.controller;

import cn.ym.domain.Param;
import cn.ym.exception.BusinessException;
import cn.ym.query.ParamQuery;
import cn.ym.service.ParamService;
import cn.ym.utils.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
/**
 * 博客参数的接口
 */
@RestController
@RequestMapping("/param")
@Api(value = "博客参数的接口的API",description="博客参数的接口的CRUD功能")
public class ParamController {

    @Autowired
    private ParamService paramService;

    //添加或修改博客参数信息
    @PutMapping
    @ApiOperation(value = "添加+修改" )
    public AjaxResult save(@RequestBody Param param, HttpSession session){
        try {
            if(param.getId()==null){
                paramService.add(param,session);
            }else {
                paramService.update(param,session);
            }
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试");
        }

    }

    //删除博客参数信息
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            paramService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }

    //查询所有博客参数信息
    @ApiOperation(value = "查询使用博客参数" )
    @GetMapping
    public List<Param> findAll(){
        return paramService.findAll();
    }

    //查询单个博客参数信息
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Param findOne(@PathVariable("id") Long id){
        return paramService.findOne(id);
    }


    //分页查询 + 高级查询
    @PostMapping
    @ApiOperation(value = "分页+高级" )
    public PageInfo<Param> queryPage(@RequestBody ParamQuery paramQuery){
        List<Param> params = paramService.queryPage(paramQuery);
        return new PageInfo<>(params);
    }

    //批量删除博客参数信息
    @ApiOperation(value = "通过ID删除" )
    @PatchMapping
    public AjaxResult Patchdel(@RequestBody Long[]  ids){
        try {
            paramService.Patchdel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();//打印错误信息
            return  new AjaxResult(false,"网络异常，请稍后重试");
        }
    }


    //获取qq--wx社交所有参数信息
    @ApiOperation(value = "获取qq--wx社交所有参数信息" )
    @GetMapping("/getAllParam")
    public Map<String,Object> getAllParam(){
        return paramService.getAllParam();
    }
}